using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

[RequireComponent(typeof(Image))]
public class ChatTrigger : MonoBehaviour,IPointerClickHandler
{
    private readonly float interval = 3f;

    private Image img;
    private ChatPanel chat;
    [SerializeField] private string[] clickTimesChatContent;
    [SerializeField] private string specialChatContent;
    private int clickTimes = 0;
    private float lastClickTime = 0;

    private void Awake()
    {
        img = GetComponent<Image>();
        img.raycastTarget = true;
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if (chat==null)
        {
            chat = GetComponentInChildren<ChatPanel>();
        }
        chat.Show(clickTimesChatContent[clickTimes]);
        lastClickTime = Time.time;
        clickTimes = clickTimes + 1 > clickTimesChatContent.Length - 1 ? 0 : clickTimes + 1;
    }

    public void SpecialTalk()
    {
        if (chat == null)
        {
            chat = GetComponentInChildren<ChatPanel>();
        }
        chat.Show(specialChatContent);
    }

    private void Update()
    {
        if (Time.time-lastClickTime>=interval)
        {
            clickTimes = 0;
            lastClickTime = Time.time;
        }
    }
}
